let express = require("express")();
let expressStatic = require("express-static");
//规划一个数据库模板
let mysql = require("mysql");

const port = 80;
let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: "123456",
    database: 'news',
    timezone: "08:00"
});
//尝试连接
sql.connect();


// express.get("/getnewsList", (request, response) => {
//     sql.query(`SELECT id,tltle FROM news`, (error, data) => {


//         //返回到前端页面
//         response.send(data)
//     })
// })
express.get("/getnews", (request, response) => {

    let id = request.query.id;
    sql.query(`SELECT * FROM news WHERE id=${id}`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");

        } else {
            response.send(data[0])

        }
    })
})

express.get("/getLogin", (request, response) => {
    sql.query(`SELECT id,tltle FROM news`, (error, data) => {


        //返回到前端页面
        response.send(data)
    })
})






//如果接收来自前端静态文件请求的话，我们要去static文件夹去找文件
express.use(expressStatic(__dirname + '/static'));
express.listen(port)